package zhizhemodi.tech.short_term_bwork.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import zhizhemodi.tech.short_term_bwork.entity.User;
import zhizhemodi.tech.short_term_bwork.service.UserService;
import zhizhemodi.tech.short_term_bwork.tool.CommonResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;


@RestController
@RequestMapping("user")
public class UserController {

    // 默认每页条数
    @Value("${default.page.rowNum}")
    private Integer ROW_MAX;

    /**
     * 导入用户表服务类
     */
    @Resource
    UserService userService;

    /**
     * 用户查询
     * @param dId
     * @param rowNum
     * @param pageNum
     * @return
     */
    @GetMapping("list")
    public CommonResult<?> getUserList(
        @RequestParam("departmentId") Integer dId,
        @RequestParam(required = false, name = "limit") Integer rowNum,
        @RequestParam(required = false, name = "page") Integer pageNum
    ) {
        List<User> users = new ArrayList<User>();
        try {
            if(rowNum == null) rowNum = ROW_MAX;
            if(pageNum == null) pageNum = 0;
            users = userService.findUsers("department_id", dId, rowNum, pageNum);
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
        Map<Object, Object> re = new HashMap<>();
        re.put("list", users);
        re.put("page", pageNum);
        return CommonResult.success(re);
    }

    /**
     * 添加用户
     * @param users
     * @return
     */
    @PostMapping("add")
    public CommonResult<?> addUser(
        @RequestBody List<User> users
    ) {
        try{
            userService.addUserList(users);
        } catch(Exception e) {
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success("添加成功");
    }
    

    /**
     * 
     * @param infoMap
     * @param userId
     * @return
     */
    @PutMapping("update")
    public CommonResult<?> updateUser(
        @RequestBody Map<String, Object> infoMap,
        @RequestParam("userId") Integer userId
    ) {
        try{
            userService.updateUserList(userId, infoMap);
        } catch(Exception e) {
            CommonResult.failed(e.getMessage());
        }
        return CommonResult.success("修改成功！");
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("delete")
    public CommonResult<?> deleteUser(
        @RequestParam("userId") Integer userId
    ) {
        try {
            userService.deleteUser(userId);
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success("删除成功");
    }
}
